/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vasthu.core.service;

import com.vasthu.api.dao.StockDAO;
import com.vasthu.api.domain.Stock;
import com.vasthu.api.service.StockManagerService;
import com.vasthu.core.service.StockManagerServiceImpl;
import com.vasthu.core.dao.StockDAOImpl;
import com.vasthu.gui.VstTableItemSelectable;
import com.vasthu.utility.VstDBManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author moremada
 */
public class StockManagerServiceImpl implements StockManagerService {

    StockDAO stockDAO;

    public StockManagerServiceImpl() {
        stockDAO = new StockDAOImpl();
    }

    @Override
    public boolean insertStock(Stock stock) {
        if (stock == null) {
            return false;
        }

        Connection con = VstDBManager.get().getConnection();
        boolean ok = false;
        try {
            ok = stockDAO.insertStock(con, stock);
        } catch (Exception ex) {
            System.out.println("ERROR : StockManagerServiceImpl->insertStock()");
        }

        VstDBManager.get().close();
        return ok;
    }

    @Override
    public Stock getStockById(long stockId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean deleteStockById(long stockId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<String> getAllStockNames() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<VstTableItemSelectable> getAllStockedItemsForTable() {
        List<Stock> stockList = new ArrayList<Stock>();
		List<VstTableItemSelectable> dataList = new ArrayList<VstTableItemSelectable>();
		
		Connection conn = VstDBManager.get().getConnection();
		
		try {
			stockList = stockDAO.getAllStockItems(conn);
			for (Stock stock : stockList) {
				dataList.add(stock);
			}
		} catch (Exception ex) {
			System.out.println("ERROR : StockManagerServiceImpl->getAllStockForTable()");
		}
		
		VstDBManager.get().close();
		
		return dataList;
    }
}
